import { Body, Controller, Delete, Get, Param, ParseIntPipe, Patch, Post, Query } from "@nestjs/common";
import { ApiOperation, ApiTags } from "@nestjs/swagger";

import { SkipJwtAuth } from "@/core/auth/decorator/skip-auth.decorator";
import { ApiBaseResponse } from "@/core/decorators/api-base-response.decorator";
import { ApiPageResponse } from "@/core/decorators/api-page-response.decorator";
import AjaxResult from "@/core/utils/AjaxResult";

import { ArticleService } from "./article.service";
import { CreateArticleDto } from "./dto/create-article.dto";
import { QueryArticleDto } from "./dto/query-article.dto";
import { UpdateArticleDto } from "./dto/update-article.dto";
import { ArticleEntity } from "./entities/article.entity";
import { ArticleDetailVO } from "./vo/article-detail.vo";

@SkipJwtAuth()
@ApiTags("文章")
@Controller("article")
export class ArticleController {
    constructor(private readonly articleService: ArticleService) {}

    @ApiOperation({ summary: "文章添加" })
    @ApiBaseResponse(ArticleEntity)
    @Post("/add")
    async create(@Body() createArticleDto: CreateArticleDto) {
        return AjaxResult.success(await this.articleService.create(createArticleDto));
    }

    @ApiOperation({ summary: "文章列表" })
    @ApiBaseResponse({ type: "array", items: ArticleEntity })
    @Get("/list")
    async findAll() {
        return AjaxResult.success(await this.articleService.findAll());
    }

    @ApiOperation({ summary: "文章分页" })
    @ApiPageResponse(ArticleEntity)
    @Get("/page")
    async page(@Query() query: QueryArticleDto) {
        return AjaxResult.success(await this.articleService.findPage(query));
    }

    @ApiOperation({ summary: "文章详情" })
    @ApiBaseResponse(ArticleDetailVO)
    @Get("detail/:id")
    async findOne(@Param("id", ParseIntPipe) id: number) {
        return AjaxResult.success(await this.articleService.findOne(id));
    }

    @ApiOperation({ summary: "文章编辑" })
    @ApiBaseResponse(ArticleEntity)
    @Patch("/edit/:id")
    async update(@Param("id", ParseIntPipe) id: number, @Body() updateArticleDto: UpdateArticleDto) {
        return AjaxResult.success(await this.articleService.update(id, updateArticleDto));
    }

    @ApiOperation({ summary: "文章删除" })
    @Delete("/delete/:id")
    async remove(@Param("id", ParseIntPipe) id: number) {
        return AjaxResult.success(await this.articleService.remove(id));
    }
}
